const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
function Promise(exector){
    let self = this
    self.status = PENDING
    self.onResolvedCallbacks = []
    self.onRejectedCallbacks = []

    function resolve(value){
        if(self.status == PENDING){
            self.status = FULFILLED
            self.value = value
            self.onResolvedCallbacks.forEach(cb => cb(self.value))
        } 
    }
    function reject(reason){
        if(self.status == PENDING){
            self.status = REJECTED
            self.value = reason
            self.onRejectedCallbacks.forEach(cb => cb(self.value))
        } 
    }

    try{
        // 因为函数执行的时候可能会出异常
        exector(resolve, reject)
    }catch(e){
        reject(e)
    }
    
}

function resolvePromise(promise2, x, resolve, reject){
    if (promise2 === x){
        return reject(new TypeError("循环引用"))
    }
    if (x instanceof Promise){
        if (x.status == PENDING){
            x.then(function(y){
                resolvePromise(promise2, y, resolve, reject)
            }, reject)
        } else {
            x.then(resolve, reject)
        }
    } else if(x!=null && ((typeof x == 'object') || (typeof x == 'function'))){
        try{
            let then = x.then
            if(typeof then == 'function'){
                then.call(x, function(y){
                    resolvePromise(promise2, y, resolve, reject)
                },function(err){
                    reject(err)
                })
            } else {
                resolve(x)
            }
        }catch(e){
            reject(e)
        }
    } else {
        resolve(x)
    }
}

Promise.prototype.then = function(onFulfilled, onRejected){
    let self = this
    let promise2; 
    // 如果成功回调没有传
    onFulfilled = typeof onFulfilled == 'function' ? onFulfilled : value => value
    onRejected = typeof onRejected == 'function' ? onRejected : reason => {throw reason}

    // 如果当前的promise已经是成功态，可以直接取值
    if(self.status == FULFILLED){
        return promise2 = new Promise(function(resolve, reject){
            try{
                // x接收then中成功回调的返回值
                let x = onFulfilled(self.value)
                // 如果获取到返回值x，会走解析promise的过程
                resolvePromise(promise2, x, resolve, reject)
            }catch(e){
                reject(e)
            }
        })
        
    }


    if(self.status == REJECTED){

        return promise2 = new Promise(function(resolve, reject){
            try{
                let x = onRejected(self.value)
                resolvePromise(promise2, x, resolve, reject)
            }catch(e){
                reject(e)
            }
        })

    }




    if(self.status == PENDING){
            return promise2 = new Promise(function(resolve, reject){
                try{
                    self.onResolvedCallbacks.push(function(){
                        let x = onFulfilled(self.value)
                        resolvePromise(promise2, x, resolve, reject)
                    })
                    self.onRejectedCallbacks.push(function(){
                        let x = onRejected(self.value)
                        resolvePromise(promise2, x, resolve, reject)
                    })
                }catch(e){
                    reject(e)
                }
            })
        
        // self.onRejectedCallbacks.push(function(){
        //     return promise2 = new Promise(function(resolve, reject){
        //         try{
        //             let x = onRejected(self.value)
        //             resolvePromise(promise2, x, resolve, reject)
        //         }catch(e){
        //             reject(e)
        //         }
        //     })
        // })
    }
}

module.exports = Promise
